class Talk {
	constructor(uimanager, talker, context, option, callback) {
		this.uimanager = uimanager;
		this.callback = callback;
		this.container = new PIXI.Container();
		
		// 寻找talker坐标
		var sprite;
		for(var i = 0; i < scene.children.length; i++)
			if(scene.children[i].name == talker) 
				sprite = scene.children[i];
				
		// 如果未找到对话者则跳过
		if(!sprite) {
			 this.callback(this);
			 return;
		}

		// 创建对话框遮罩
		const frameMask = new PIXI.Graphics();
		frameMask.beginFill(0x000000, 0.000001);
		frameMask.drawRect(0, 0, 8000, 8000);
		frameMask.endFill();

		// 创建对话框
		const frame = new PIXI.smooth.SmoothGraphics();
		frame.beginFill(0x000000, 0.7);
		frame.lineStyle({ color: 0xffffff, width: 1, alignment: 0 });
		frame.moveTo(20,  0).lineTo(350, 10).lineTo(380, 40).
			lineTo(350, 95).lineTo(198, 98).lineTo(196, 110).
			lineTo(180, 118).lineTo(187, 108).lineTo(185, 99).
			lineTo(20, 100).lineTo(0, 60).lineTo(20, 0);
			
		const frameX = (sprite.x - camera.pivot.x) * camera.scale.x + camera.x - 168;
		const frameY = (sprite.y - camera.pivot.y) * camera.scale.y + camera.y - 133;
		frame.position.set(frameX, frameY);
		
		// 设置对话位置
		const style = new PIXI.TextStyle({
		    fontFamily: 'Arial',
		    fontSize: 16,
		    fill: ['#ffffff'], // gradient
		    wordWrap: true,
			breakWords:true,
		    wordWrapWidth: 240,
		    lineJoin: 'round',
		});
		
		const richTextSize = PIXI.TextMetrics.measureText(context, style);
		const richText = new PIXI.Text(context, style);
		richText.x = 0;
		richText.y = 0;
		richText.position.set(frame.position.x + 30, frame.position.y + (100 - richTextSize.height) / 2);
		
		// 添加一个对话框容器
		this.container.addChild(frameMask);
		this.container.addChild(frame);
		this.container.addChild(richText);
		this.container.eventMode = 'static';
		//this.container.cursor = 'pointer';
		this.container.on('pointerdown', (event) => {
			this.end();
		});
		
		this.uimanager.addChild(this.container);
	}
	
	end () {
		this.uimanager.removeChild(this.container);
		if (this.callback) this.callback(this);
	}
} 

class Menu {
	constructor(uimanager, items, talker, context, option, callback) {
		this.uimanager = uimanager;
		this.callback = callback;
		this.container = new PIXI.Container();
		
		// 绘制最后一个对话
		// 寻找talker坐标
		var sprite;
		for(var i = 0; i < scene.children.length; i++)
			if(scene.children[i].name == talker) 
				sprite = scene.children[i];
				
		// 如果未找到对话者则跳过
		if(!sprite) {
			 this.callback(this);
			 return;
		}
		
		// 得到talker半身像
		// const spriteImage = new PIXI.Sprite(sprite.image);
		
		// 创建对话框遮罩
		const frameMask = new PIXI.Graphics();
		frameMask.beginFill(0x000000, 0.000001);
		frameMask.drawRect(0, 0, 8000, 8000);
		frameMask.endFill();
		
		// 创建对话框
		const frame = new PIXI.smooth.SmoothGraphics();
		frame.beginFill(0x000000, 0.7);
		frame.lineStyle({ color: 0xffffff, width: 1, alignment: 0 });
		frame.moveTo(20,  0).lineTo(350, 10).lineTo(380, 40).
			lineTo(350, 95).lineTo(198, 98).lineTo(196, 110).
			lineTo(180, 118).lineTo(187, 108).lineTo(185, 99).
			lineTo(20, 100).lineTo(0, 60).lineTo(20, 0);
			
		const frameX = (sprite.x - camera.pivot.x) * camera.scale.x + camera.x - 168;
		const frameY = (sprite.y - camera.pivot.y) * camera.scale.y + camera.y - 133;
		frame.position.set(frameX, frameY);
		
		// 设置对话位置
		const style = new PIXI.TextStyle({
		    fontFamily: 'Arial',
		    fontSize: 16,
		    fill: ['#ffffff'], // gradient
		    wordWrap: true,
			breakWords:true,
		    wordWrapWidth: 240,
		    lineJoin: 'round',
		});
		
		const richTextSize = PIXI.TextMetrics.measureText(context, style);
		const richText = new PIXI.Text(context, style);
		richText.x = 0;
		richText.y = 0;
		richText.position.set(frame.position.x + 30, frame.position.y + (100 - richTextSize.height) / 2);
		
		// 添加一个对话框容器
		this.container.addChild(frameMask);
		this.container.addChild(frame);
		this.container.addChild(richText);
	
		var offsetY = frameY + 105;
		for(var i = 0; i < items.length; i++) {
			var item = items[i];
			
			// button框
			const frame = new PIXI.smooth.SmoothGraphics();
			frame.index = i;
			frame.beginFill(0x000000, 0.7);
			frame.lineStyle({ color: 0xffffff, width: 1, alignment: 0 });
			frame.moveTo(0,0).lineTo(160, 0).lineTo(170, 52).lineTo(10, 52).lineTo(0, 0);
			frame.on('pointerdown', (evt) => {
				this.callback(evt.currentTarget.index);
				this.uimanager.removeChild(this.container);
			});
			frame.eventMode = 'static';
			frame.cursor = 'pointer';
			frame.position.set(frameX + 210, offsetY);
			
			// 设置文字
			const style = new PIXI.TextStyle({
			    fontFamily: 'Arial',
			    fontSize: 16,
			    fill: ['#ffffff'], // gradient
			    wordWrap: true,
			    wordWrapWidth: 140,
				breakWords:true,
			    lineJoin: 'round',
			});
			
			const richTextSize = PIXI.TextMetrics.measureText(item, style);
			const richText = new PIXI.Text(item, style);
			richText.x = 0;
			richText.y = 0;
			richText.position.set(frame.position.x + (170 - richTextSize.width) / 2, frame.position.y + (52 - richTextSize.height) / 2);		
			
			this.container.addChild(frame);
			this.container.addChild(richText);
			
			offsetY = offsetY + 62;
		}
		
		this.uimanager.addChild(this.container);
	}
}

class UIManager extends PIXI.Container {
	constructor() {
		super();
	}
	
	talk (talker, context, option) {
		this.lastTalker = talker;
		this.lastContext = context;
		return new Promise(resolve => new Talk(this, talker, context, option, resolve));
	}
	
	menu (items, option) {
		return new Promise(resolve => new Menu(this, items, this.lastTalker, this.lastContext, option, resolve));
	} 

	update() {
		for(var i = 0; i < this.children.length; i++)
			if(this.children[i].update)
				this.children[i].update();
	}
}

L2D.UIManager = new UIManager();
L2D.UserInterface = new PIXI.Container();